package com.jiang.factory;

import java.util.Properties;

/**
 * @author jiangjingbo
 * @version V1.0
 * @Project java2203
 * @Package com.jiang.factory
 * @Description:
 * @Date 2022/6/2 10:00
 */
public class Mybeanfactory {
    /**
     *
     */
    private Properties properties = new Properties();

    public Mybeanfactory() {
    }

    public Mybeanfactory(String config) throws Exception {
        //通过流读取根据了配置文件读取名字和路径
        properties.load(Mybeanfactory.class.getResourceAsStream(config));
    }

    /**
     * 获取java类对象, 也就是JavaBean对象, 对象由这个工厂产生
     */
    public Object getBean(String beanName) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        //获取类路径 也就是类的权限顶名字
        String property = properties.getProperty(beanName);
        //2. 判断这个路径是否为空, 如果不为空再用反射获取类对象
        if(property!=null){
            Class clazz = Class.forName(property);
            return clazz.newInstance();
        }
        return null;
    }
}
